package databasetest;
//수정함~
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

public class DatabaseTest extends JFrame {

	String driver ="oracle.jdbc.driver.OracleDriver";
	String url ="jdbc:oracle:thin:@127.0.0.1:1521:xe";
	String db_user = "hr";
	String db_pwd = "hr";
	Connection conn;
	Insert insert = null;
	Update update = null;
	Delete delete = null;
	Select select = null;
	
	private JPanel contentPane;
	private JMenuBar menuBar;
	private JMenu mnNewMenu;
	private JMenuItem mntmNewMenuItem;
	private JMenuItem mntmNewMenuItem_1;
	private JMenuItem mntmNewMenuItem_2;
	private JMenuItem mntmNewMenuItem_3;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DatabaseTest frame = new DatabaseTest();
					
					UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
//					UIManager.setLookAndFeel("com.jTattoo.plaf.mcwin.McWinLookAndFeel");
					SwingUtilities.updateComponentTreeUI(frame);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public DatabaseTest() {
		setTitle("데이터베이스 연습");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 902, 700);
		setJMenuBar(getMenuBar_1());
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		try {
			// 데이터베이스 연결 테스트
			// 1)드라이버로딩
			Class.forName(driver).newInstance();
			System.out.println("에어컨이 작동하였습니다. 추웡");
			System.out.println("감기는 나아가는데 목이 쉬었네 말을 아껴야겟다");
			// 2)Connection 객체 생성
			conn = DriverManager.getConnection(url, db_user, db_pwd);
			System.out.println("데이터베이스가 정상적으로 연결되었습니다.");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	public void openInsert(){
		if(insert == null){
			insert = new Insert(conn,this);
			insert.setBounds(10, 10, 800, 100);
			contentPane.add(insert);
		}
		insert.setVisible(true);
		insert.toFront();
	}
	
	public void openUpdate(){
		if(update == null){
			update = new Update(conn,this);
			update.setBounds(10, 120, 800, 100);
			contentPane.add(update);
		}
		update.setVisible(true);
		update.toFront();
	}
	
	public void openDelete(){

		if(delete == null){
			delete = new Delete(conn,this);
			delete.setBounds(10, 230, 800, 100);
			contentPane.add(delete);
		}
		delete.setVisible(true);
		delete.toFront();
		delete.toFront();
	}
	
	public void openSelect(){
		if(select == null){
			select = new Select(conn,this);
			select.setBounds(10, 340, 800, 250);
			contentPane.add(select);
		}
		select.setVisible(true);
		select.toFront();
	}

	private JMenuBar getMenuBar_1() {
		if (menuBar == null) {
			menuBar = new JMenuBar();
			menuBar.add(getMnNewMenu());
		}
		return menuBar;
	}
	private JMenu getMnNewMenu() {
		if (mnNewMenu == null) {
			mnNewMenu = new JMenu("New menu");
			mnNewMenu.add(getMntmNewMenuItem());
			mnNewMenu.add(getMntmNewMenuItem_1());
			mnNewMenu.add(getMntmNewMenuItem_2());
			mnNewMenu.add(getMntmNewMenuItem_3());
		}
		return mnNewMenu;
	}
	private JMenuItem getMntmNewMenuItem() {
		if (mntmNewMenuItem == null) {
			mntmNewMenuItem = new JMenuItem("입력");
			mntmNewMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					openInsert();
				}
			});
		}
		return mntmNewMenuItem;
	}
	private JMenuItem getMntmNewMenuItem_1() {
		if (mntmNewMenuItem_1 == null) {
			mntmNewMenuItem_1 = new JMenuItem("수정");
			mntmNewMenuItem_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					openUpdate();
				}
			});
		}
		return mntmNewMenuItem_1;
	}
	private JMenuItem getMntmNewMenuItem_2() {
		if (mntmNewMenuItem_2 == null) {
			mntmNewMenuItem_2 = new JMenuItem("삭제");
			mntmNewMenuItem_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					openDelete();
				}
			});
		}
		return mntmNewMenuItem_2;
	}
	private JMenuItem getMntmNewMenuItem_3() {
		if (mntmNewMenuItem_3 == null) {
			mntmNewMenuItem_3 = new JMenuItem("조회");
			mntmNewMenuItem_3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					openSelect();
				}
			});
		}
		return mntmNewMenuItem_3;
	}
}
